﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Kinect_Interface_Prototype_1.Interface_Design
{
    public partial class AppDock : Form
    {
        StartupProgram MainProgram;
        private List<PictureBox> appDockPictureBoxes;
        Point mouseOffset = new Point();
        private bool mouseDown = false;
        IniFile configINI;
        IniFile settingsINI;
        Timer appTimer = new Timer();

        private Color transparencyColor = Color.Aqua;


        public AppDock(StartupProgram program, IniFile configINI, IniFile settingsINI)
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            MainProgram = program;
            this.configINI = configINI;
            this.settingsINI = settingsINI;
        }

        private void appDock_Load(object sender, EventArgs e)
        {
            this.BackColor = transparencyColor;
            this.TransparencyKey = transparencyColor;

            int screenWidth = Screen.PrimaryScreen.Bounds.Width;
            int screenHeight = Screen.PrimaryScreen.Bounds.Height;

            int x = Int32.Parse(configINI.IniReadValue("ApplicationDock", "startX"));
            int y = Int32.Parse(configINI.IniReadValue("ApplicationDock", "startY"));

            int newX = x + this.Width + MainProgram.ControlForm_Width() > screenWidth || x < 0 ? screenWidth - this.Width - MainProgram.ControlForm_Width() : x;
            int newY = y + this.Height > screenHeight ? (y < 0 ? 0 : screenHeight - this.Height) : (y < 0 ? 0 : y);

            this.Location = new Point(newX, newY);


            appTimer.Interval = 100;
            appTimer.Tick += new EventHandler(appTimer_Tick);


            appDockPictureBoxes = new List<PictureBox> { appPictureBox1, appPictureBox2, appPictureBox3, appPictureBox4, appPictureBox5 };

            SetupAppDock();
        }

        private void AppDockForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset = new Point(-e.X - SystemInformation.FrameBorderSize.Width, -e.Y - SystemInformation.FrameBorderSize.Height);
                mouseDown = true;
            }
        }

        private void AppDockForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDown = false;
        }

        private void AppDockForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                Point mousePosition = Control.MousePosition;
                mousePosition.Offset(mouseOffset.X, mouseOffset.Y);
                this.Location = mousePosition;
            }
        }

        private void appPictureBox_Click(object sender, EventArgs e)
        {
            if (!timerStarted)
            {
                appTimer.Tag = ((PictureBox)sender).Tag;
                appTimer.Start();
                timerStarted = true;
            }
        }

        bool timerStarted = false;
        int timerCounter = 0;
        private void appTimer_Tick(object sender, EventArgs e)
        {
            AppDockItem app = (AppDockItem)appTimer.Tag;
            changeImage(app.PictureBox);
        }

        private void changeImage(PictureBox pictureBox)
        {
            int center = pictureBox.Location.Y + pictureBox.Height/2;

            pictureBox.Size = timerCounter < 5
                                    ? new Size((int)(pictureBox.Size.Width - 7), (int)(pictureBox.Size.Height - 7))
                                    : new Size((int)(pictureBox.Size.Width + 7), (int)(pictureBox.Size.Height + 7));

            pictureBox.Location = new Point(this.Width / 2 - pictureBox.Width / 2, center - pictureBox.Height / 2);
            timerCounter++;

            if (timerCounter == 10)
            {
                timerCounter = 0;
                appTimer.Stop();
                timerStarted = false;
                ((AppDockItem)appTimer.Tag).onClick(MainProgram);
            }
        }

        private void AppDock_FormClosing(object sender, FormClosingEventArgs e)
        {
            configINI.IniWriteValue("ApplicationDock", "startX", this.Location.X.ToString());
            configINI.IniWriteValue("ApplicationDock", "startY", this.Location.Y.ToString());
        }

        private void SetupAppDock()
        {
            foreach (PictureBox appDockPictureBox in appDockPictureBoxes)
            {
                int index = appDockPictureBoxes.IndexOf(appDockPictureBox) + 1;
                int center = appDockPictureBox.Location.Y + appDockPictureBox.Height / 2;
                PredefinedAppType type = (PredefinedAppType)PredefinedAppType.Parse(typeof(PredefinedAppType), settingsINI.IniReadValue("App" + index, "type"));

                if (type == PredefinedAppType.MicrosoftWord)
                {
                    appDockPictureBox.Tag = new AppDockItem(AppType.SystemApp, "WINWORD.EXE", appDockPictureBox);

                    using (FileStream fs = new FileStream(Constants.WordIconImage, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                }
                else if (type == PredefinedAppType.MicrosoftPowerPoint)
                {
                    appDockPictureBox.Tag = new AppDockItem(AppType.SystemApp, "POWERPNT.EXE", appDockPictureBox);

                    using (FileStream fs = new FileStream(Constants.PowerPointIconImage, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                }
                else if (type == PredefinedAppType.KinectKeyboard)
                {
                    appDockPictureBox.Tag = new AppDockItem(AppType.Keyboard, appDockPictureBox);

                    using (FileStream fs = new FileStream(Constants.KeyboardIconImage, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                }
                else if (type == PredefinedAppType.KinectMediaPlayer)
                {
                    appDockPictureBox.Tag = new AppDockItem(AppType.MediaPlayer, appDockPictureBox);

                    using (FileStream fs = new FileStream(Constants.KinectMediaPlayerIconImage, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                }
                else if (type == PredefinedAppType.KinectFolderControl)
                {
                    appDockPictureBox.Tag = new AppDockItem(AppType.FolderControl, appDockPictureBox);

                    using (FileStream fs = new FileStream(Constants.FolderControlIconImage, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                }
                else if (type == PredefinedAppType.CustomApplication)
                {
                    string path = settingsINI.IniReadValue("App" + index, "filePath");
                    string imagePath = Constants.InterfaceImagesFolder + settingsINI.IniReadValue("App" + index, "imageName");

                    appDockPictureBox.Tag = new AppDockItem(AppType.SystemApp, path, appDockPictureBox);

                    // Set to default icon if the image doesn't exist
                    imagePath = File.Exists(imagePath) ? imagePath : Constants.InterfaceImagesFolder + "defaultIcon.png";
                    
                    using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                    {
                        Image image = Image.FromStream(fs);
                        appDockPictureBox.Width = image.Width < 74 ? image.Width : 74;
                        appDockPictureBox.Height = image.Height < 81 ? image.Height : 81;

                        appDockPictureBox.Image = image;
                    }
                    
                }

                appDockPictureBox.Location = new Point(this.Width / 2 - appDockPictureBox.Width / 2, center - appDockPictureBox.Height / 2);
            }
        }

        // Public Methods
        public void RefreshAppDock()
        {
            SetupAppDock();
        }
        
        public void ResetLocation()
        {
            this.SetDesktopLocation(Screen.PrimaryScreen.Bounds.Width / 2 - this.Width, Screen.PrimaryScreen.Bounds.Height / 2 - this.Height/2);
        }

        public void AppClick(int appNumber)
        {
            appPictureBox_Click(appDockPictureBoxes[appNumber - 1], new EventArgs());
        }
    }
}
